package com.itcam.cm.manager.service.impl;

import com.itcam.cm.manager.mapper.SysRoleMapper;
import com.itcam.cm.manager.mapper.SysRoleMenuMapper;
import com.itcam.cm.manager.service.SysMenuService;
import com.itcam.cm.manager.service.SysRoleMenuService;
import com.itcam.cm.model.dto.system.AssginMenuDto;
import com.itcam.cm.model.entity.system.SysMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysRoleMenuServiceImpl implements SysRoleMenuService {

    @Autowired
    SysRoleMenuMapper sysRoleMenuMapper;

    @Autowired
    SysMenuService sysMenuService;


    @Override
    public Map<String, Object> findSysRoleMenuByRoleId(Long roleId) {

        // 查询菜单树
        List<SysMenu> tree = sysMenuService.findNodes();

        // 查询当前角色已经选中的菜单id
        List<Long> ids = sysRoleMenuMapper.selectSysRoleMenuIdsByRoleId(roleId);

        // 封装
        Map<String,Object> map = new HashMap<>();
        map.put("sysMenuList",tree);
        map.put("roleMenuIds",ids);
        return map;
    }

    @Override
    public void doAssign(AssginMenuDto assginMenuDto) {
        System.out.println("分配");

        // 先删除
        sysRoleMenuMapper.deleteByRoleId(assginMenuDto.getRoleId());

        // 后添加
        sysRoleMenuMapper.insertRoleMenus(assginMenuDto);

    }
}
